package com.wangzhixuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangzhixuan.commons.result.Tree;
import com.wangzhixuan.commons.utils.BeanUtils;
import com.wangzhixuan.mapper.OrganizationMapper;
import com.wangzhixuan.model.Organization;
import com.wangzhixuan.model.vo.OrganizationVo;
import com.wangzhixuan.service.IOrganizationService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Organization 表数据服务层接口实现类
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements IOrganizationService {

    @Override
    public List<Tree> selectTree() {
        List<Organization> organizationList = selectAll();
        List<Tree> trees = new ArrayList<>();
        if (CollectionUtils.isEmpty(organizationList)) {
            return trees;
        }
        organizationList.forEach(organization -> {
            if (Objects.isNull(organization.getPid()) || organization.getPid() == 0) {
                Tree tree = convertTree(organization);
                tree.setChildren(buildChildren(organizationList, tree.getId()));
                trees.add(tree);
            }
        });
        return trees;
    }

    private List<Tree> buildChildren(List<Organization> resourceList, Long pid) {
        List<Tree> childrens = new ArrayList<>();
        resourceList.forEach(res -> {
            if (pid.equals(res.getPid())) {
                Tree tree = convertTree(res);
                tree.setChildren(buildChildren(resourceList, tree.getId()));
                childrens.add(tree);
            }
        });
        if (childrens.isEmpty()) {
            return null;
        }
        return childrens;
    }

    private List<OrganizationVo> buildChildrens(List<Organization> organizationList, Long pid) {
        List<OrganizationVo> childrens = new ArrayList<>();
        organizationList.forEach(res -> {
            if (pid.equals(res.getPid())) {
                OrganizationVo vo = BeanUtils.copy(res,OrganizationVo.class);
                vo.setChildrens(buildChildrens(organizationList, res.getId()));
                childrens.add(vo);
            }
        });
        if (childrens.isEmpty()) {
            return null;
        }
        return childrens;
    }

    private Tree convertTree(Organization organization) {
        Tree tree = new Tree();
        tree.setId(organization.getId());
        tree.setText(organization.getName());
        tree.setIconCls(organization.getIcon());
        tree.setPid(organization.getPid());
        tree.setAttributes(organization.getAddress());
        return tree;
    }

    private List<Organization> selectAll(){
        QueryWrapper<Organization> wrapper = new QueryWrapper<>();
        wrapper.orderBy(false, true, "seq");
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<OrganizationVo> selectTreeGrid() {
        List<Organization> organizationList = selectAll();
        List<OrganizationVo> treeGrid = new ArrayList<>();
        if (CollectionUtils.isEmpty(organizationList)) {
            return treeGrid;
        }
        organizationList.forEach(organization -> {
            if (Objects.isNull(organization.getPid()) || organization.getPid() == 0) {
                OrganizationVo vo = BeanUtils.copy(organization,OrganizationVo.class);
                vo.setChildrens(buildChildrens(organizationList, organization.getId()));
                treeGrid.add(vo);
            }
        });
        return treeGrid;
    }


}